<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">权限管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="adduserrightVisible = true"
            >添加角色</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="rolelist" border stripe>
        <el-table-column type="expand">
          <template v-slot="scope">
            <el-row v-for="item1 in scope.row.children" :key="item1.id">
              <el-col :span="5">
                <el-tag>{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="item2 in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag>{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag v-for="item3 in item2.children" :key="item3.id">{{
                      item3.authName
                    }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template v-slot="scope">
            <el-button size="mini" type="primary">编辑</el-button>
            <el-button size="mini" type="warning" @click="showright(scope.row)"
              >分配权限</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="removeuserright(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配角色 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showrightVisible"
      width="50%"
      @close="showrightclose"
    >
      <el-tree
        :data="rihtlist"
        :props="rihtlistProps"
        show-checkbox
        default-expand-all="true"
        node-key="id"
        :default-checked-keys="defKeys"
        :ref="treeref"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showrightVisible = false">取 消</el-button>
        <el-button type="primary" @click="showrightVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 添加用户 -->
    <el-dialog
      title="添加用户"
      :visible.sync="adduserrightVisible"
      width="50%"
      @close="adduserrighclose"
    >
      <el-form
        :model="adduserrightForm"
        ref="adduserrightref"
        label-width="100px"
      >
        <el-form-item label="角色名称">
          <el-input v-model="adduserrightForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="adduserrightForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adduserrightVisible = false">取 消</el-button>
        <el-button type="primary" @click="adduserright">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolelist: [],
      showrightVisible: false,
      rihtlist: [],
      adduserrightVisible: false,
      adduserrightForm: {
        roleName: "",
        roleDesc: "",
      },
      rihtlistProps: {
        children: "children",
        label: "authName",
      },
      defKeys: [],
      roleid: "",
    };
  },
  created() {
    this.getrolelist();
  },
  methods: {
    async getrolelist() {
      const { data: res } = await this.$http.get("roles");
      this.rolelist = res.data;
    },
    async showright(role) {
      this.roleid = role.id;
      const { data: res } = await this.$http.get("rights/tree");
      this.rihtlist = res.data;
      this.getleafkey(role, this.defKeys);
      this.showrightVisible = true;
    },
    async adduserright() {
      const { data: res } = await this.$http.post(
        "roles",
        this.adduserrightForm
      );
      this.adduserrightForm = res.data;
      this.$message.success("添加用户成功");
      this.getrolelist();
      this.adduserrightVisible = false;
    },
    adduserrighclose() {
      this.$refs.adduserrightref.resetFields();
    },
    // 删除用户
    async removeuserright(id) {
      //确认删除会显示confirm
      const result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (result !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete(`roles/${id}`);
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户失败");
      }
      this.$message.success("删除用户成功");
      this.getrolelist();
    },
    getleafkey(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => {
        this.getleafkey(item, arr);
      });
    },
    showrightclose() {
      this.defKeys = [];
    },
    // async allright() {
    //   const keys = [
    //     ...this.$refs.treeref.getCheckedKeys(),
    //     ...this.$refs.treeref.getHalfCheckedKeys(),
    //   ];
    //   const idstr = keys.join(",");
    //   const { data: res } = await this.$http.post(
    //     `roles/${this.roleid}/rights`,
    //     {
    //       rids: idstr,
    //     }
    //   );
    //   console.log(res);
    //   if (res.meta.status !== 200) {
    //     return this.$message.error("分配权限失败");
    //   }
    //   this.$message.success("分配权限成功");
    //   this.showrightVisible = false;
    // },
  },
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 5px;
}
</style>
